package com.example.hxds.bff.driver.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.example.hxds.bff.driver.controller.form.*;
import com.example.hxds.bff.driver.feign.DriverServiceApi;
import com.example.hxds.bff.driver.feign.OrderServiceApi;
import com.example.hxds.bff.driver.service.DriverService;
import com.example.hxds.common.util.CosUtil;
import com.example.hxds.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

@Service
public class DriverServiceImpl implements DriverService {

    @Autowired
    DriverServiceApi driverServiceApi;
    @Autowired
    OrderServiceApi orderServiceApi;

    @Override
    @Transactional
    @LcnTransaction
    public long registerNewDriver(RegisterNewDriverForm newDriverForm) {
        R r = driverServiceApi.registerNewDriver(newDriverForm);
        return Convert.toLong(r.get("driverId"));
    }

    @Override
    @Transactional
    @LcnTransaction
    public int updateDriverAuth(UpdateDriverAuthForm form) {
        R r = driverServiceApi.updateDriverAuth(form);
        Object rows = r.get("rows");
        return Convert.toInt(rows);
    }

    @Override
    @Transactional
    @LcnTransaction
    public HashMap login(LoginForm form) {
        R r = driverServiceApi.login(form);
        return (HashMap) r.get("result");
    }


    @Override
    public HashMap searchDriverBaseInfo(SearchDriverBaseInfoForm form) {
        R r = driverServiceApi.searchDriverBaseInfo(form);
        HashMap map = (HashMap) r.get("result");
        return map;
    }

    @Override
    public HashMap searchWorkbenchData(long driverId) {
        //查询司机当天业务数据
        SearchDriverTodayBusinessDataForm form1=new SearchDriverTodayBusinessDataForm();
        form1.setDriverId(driverId);
        R r = orderServiceApi.searchDriverTodayBusinessData(form1);
        HashMap business = (HashMap) r.get("result");

        SearchDriverSettingsForm form2=new SearchDriverSettingsForm();
        form2.setDriverId(driverId);
        R r2 = driverServiceApi.searchDriverSettings(form2);
        HashMap settings = (HashMap) r2.get("result");

        return new HashMap(){
            {
                put("business",business);
                put("settings",settings);
            }
        };
    }

    @Autowired
    private CosUtil cosUtil;

    @Override
    public HashMap searchDriverAuth(SearchDriverAuthForm form) {
        R r = driverServiceApi.searchDriverAuth(form);
        HashMap map = (HashMap) r.get("result");
        //获取私有读写文件的临时URL地址
        String idcardFront = MapUtil.getStr(map, "idcardFront");
        String idcardBack = MapUtil.getStr(map, "idcardBack");
        String idcardHolding = MapUtil.getStr(map, "idcardHolding");
        String drcardFront = MapUtil.getStr(map, "drcardFront");
        String drcardBack = MapUtil.getStr(map, "drcardBack");
        String drcardHolding = MapUtil.getStr(map, "drcardHolding");
        String idcardFrontUrl = idcardFront.length() > 0 ? cosUtil.getPrivateFileUrl(idcardFront) : "";
        String idcardBackUrl = idcardBack.length() > 0 ? cosUtil.getPrivateFileUrl(idcardBack) : "";
        String idcardHoldingUrl = idcardHolding.length() > 0 ? cosUtil.getPrivateFileUrl(idcardHolding) : "";
        String drcardFrontUrl = drcardFront.length() > 0 ? cosUtil.getPrivateFileUrl(drcardFront) : "";
        String drcardBackUrl = drcardBack.length() > 0 ? cosUtil.getPrivateFileUrl(drcardBack) : "";
        String drcardHoldingUrl = drcardHolding.length() > 0 ? cosUtil.getPrivateFileUrl(drcardHolding) : "";
        map.put("idcardFrontUrl", idcardFrontUrl);
        map.put("idcardBackUrl", idcardBackUrl);
        map.put("idcardHoldingUrl", idcardHoldingUrl);
        map.put("drcardFrontUrl", drcardFrontUrl);
        map.put("drcardBackUrl", drcardBackUrl);
        map.put("drcardHoldingUrl", drcardHoldingUrl);
        return map;
    }
}
